<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能客服系统</title>
    <style>
        :root {
            --primary-color: #4361ee;
            --secondary-color: #3f37c9;
            --accent-color: #4895ef;
            --light-color: #f8f9fa;
            --dark-color: #212529;
            --success-color: #4cc9f0;
            --warning-color: #f72585;
            --border-radius: 12px;
        }
        
        body {
            font-family: 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
            max-width: 900px;
            margin: 0 auto;
            padding: 20px;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .container {
            background-color: white;
            padding: 30px;
            border-radius: var(--border-radius);
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            margin-top: 30px;
        }
        
        h1 {
            color: var(--primary-color);
            text-align: center;
            margin-bottom: 30px;
            font-weight: 600;
            font-size: 2.2rem;
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .input-section {
            margin-bottom: 25px;
            position: relative;
        }
        
        .input-section label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: var(--dark-color);
        }
        
        textarea {
            width: 100%;
            height: 120px;
            padding: 15px;
            border: 2px solid #e9ecef;
            border-radius: var(--border-radius);
            resize: vertical;
            font-size: 1rem;
            transition: all 0.3s ease;
        }
        
        textarea:focus {
            border-color: var(--accent-color);
            outline: none;
            box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.2);
        }
        
        button {
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            color: white;
            padding: 12px 25px;
            border: none;
            border-radius: var(--border-radius);
            cursor: pointer;
            margin-top: 15px;
            font-size: 1rem;
            font-weight: 500;
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        
        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(0,0,0,0.15);
        }
        
        button:active {
            transform: translateY(0);
        }
        
        .response {
            margin-top: 30px;
            padding: 20px;
            background-color: white;
            border-radius: var(--border-radius);
            box-shadow: 0 4px 12px rgba(0,0,0,0.08);
            border-left: 5px solid var(--success-color);
        }
        
        .loading {
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
            color: var(--dark-color);
        }
        
        .loading:after {
            content: "...";
            animation: dots 1.5s steps(5, end) infinite;
            margin-left: 5px;
        }
        
        @keyframes dots {
            0%, 20% { content: "."; }
            40% { content: ".."; }
            60%, 100% { content: "..."; }
        }
        
        .suggestions {
            margin-top: 20px;
            padding: 20px;
            background-color: white;
            border-radius: var(--border-radius);
            box-shadow: 0 4px 12px rgba(0,0,0,0.08);
        }
        
        .suggestions h3 {
            color: var(--primary-color);
            margin-top: 0;
            margin-bottom: 15px;
        }
        
        .suggestions ul {
            list-style-type: none;
            padding: 0;
            margin: 0;
        }
        
        .suggestions li {
            padding: 10px 15px;
            cursor: pointer;
            color: var(--primary-color);
            border-radius: 6px;
            margin-bottom: 5px;
            transition: all 0.2s ease;
        }
        
        .suggestions li:hover {
            background-color: rgba(67, 97, 238, 0.1);
        }
        
        .feedback-section {
            margin-top: 30px;
            padding: 20px;
            background-color: white;
            border-radius: var(--border-radius);
            box-shadow: 0 4px 12px rgba(0,0,0,0.08);
        }
        
        .feedback-section h4 {
            margin-top: 0;
            margin-bottom: 15px;
            color: var(--dark-color);
        }
        
        .rating-stars {
            display: flex;
            justify-content: center;
            margin: 15px 0;
            gap: 10px;
        }
        
        .rating-stars span {
            font-size: 28px;
            color: #e0e0e0;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        
        .rating-stars span:hover, 
        .rating-stars span.active {
            color: var(--warning-color);
            transform: scale(1.2);
        }
        
        .average-rating {
            margin-top: 15px;
            font-weight: 600;
            color: var(--success-color);
            text-align: center;
            font-size: 1.1rem;
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                padding: 20px;
            }
            
            h1 {
                font-size: 1.8rem;
            }
            
            textarea {
                height: 100px;
            }
        }
        
        /* 客服头像样式 */
        .chat-message {
            display: flex;
            margin-bottom: 15px;
        }
        
        .chat-message .avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background-color: var(--accent-color);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 15px;
            font-weight: bold;
        }
        
        .chat-message .content {
            flex: 1;
        }
        
        .chat-message .name {
            font-weight: 600;
            margin-bottom: 5px;
            color: var(--dark-color);
        }
        
        .chat-message .text {
            background-color: var(--light-color);
            padding: 12px 15px;
            border-radius: 0 var(--border-radius) var(--border-radius) var(--border-radius);
            display: inline-block;
            max-width: 80%;
        }
        
        .user-message {
            justify-content: flex-end;
        }
        
        .user-message .avatar {
            background-color: var(--primary-color);
            margin-right: 0;
            margin-left: 15px;
        }
        
        .user-message .text {
            background-color: var(--primary-color);
            color: white;
            border-radius: var(--border-radius) 0 var(--border-radius) var(--border-radius);
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>智能客服系统</h1>
        <div class="input-section">
            <label for="question">请输入您的问题：</label>
            <textarea id="question" placeholder="例如：如何退货？ 你们的营业时间是什么时候？" oninput="showSuggestions()" onkeydown="handleKeyDown(event)"></textarea>
            <button onclick="askQuestion()">提交问题</button>
        </div>
        <div id="suggestions" class="suggestions" style="display: none;">
            <h3>您可能想问：</h3>
            <ul id="suggestion-list"></ul>
        </div>
        <div id="response" class="response" style="display: none;"></div>
    </div>

    <script>
        async function showSuggestions() {
            const question = document.getElementById('question').value.trim();
            const suggestionsDiv = document.getElementById('suggestions');
            const suggestionList = document.getElementById('suggestion-list');
            
            if (!question) {
                suggestionsDiv.style.display = 'none';
                return;
            }

            try {
                const response = await fetch(`/api/customer-service/suggestions?query=${encodeURIComponent(question)}`);
                if (response.ok) {
                    const matchedQuestions = await response.json();
                    
                    if (matchedQuestions.length > 0) {
                        suggestionList.innerHTML = '';
                        matchedQuestions.forEach(q => {
                            const li = document.createElement('li');
                            li.textContent = q;
                            li.onclick = () => {
                                document.getElementById('question').value = q;
                                suggestionsDiv.style.display = 'none';
                            };
                            suggestionList.appendChild(li);
                        });
                        suggestionsDiv.style.display = 'block';
                    } else {
                        suggestionsDiv.style.display = 'none';
                    }
                }
            } catch (error) {
                console.error('获取推荐问题失败:', error);
                suggestionsDiv.style.display = 'none';
            }
        }

        let currentConversationId = null;

        async function askQuestion() {
            const question = document.getElementById('question').value;
            const responseElement = document.getElementById('response');
            const suggestionsDiv = document.getElementById('suggestions');
            
            if (!question.trim()) {
                alert('请输入问题');
                return;
            }
            
            // 隐藏推荐问题
            suggestionsDiv.style.display = 'none';
            
            // 显示加载状态
            responseElement.style.display = 'block';
            responseElement.innerHTML = '<p class="loading">正在为您解答...</p>';
            
            try {
                const requestData = {
                    question: question,
                    conversationId: currentConversationId
                };

                const response = await fetch('/api/customer-service/ask', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(requestData)
                });
                
                if (response.ok) {
                    const result = await response.json();
                    currentConversationId = result.conversationId;
                    responseElement.innerHTML = `
                        <div class="chat-message">
                            <div class="avatar">AI</div>
                            <div class="content">
                                <div class="name">智能客服</div>
                                <div class="text">${result.answer}</div>
                            </div>
                        </div>
                        <div class="feedback-section">
                            <h4>请评价本次服务：</h4>
                            <div class="rating-stars">
                                <span onclick="rateService(1)">☆</span>
                                <span onclick="rateService(2)">☆</span>
                                <span onclick="rateService(3)">☆</span>
                                <span onclick="rateService(4)">☆</span>
                                <span onclick="rateService(5)">☆</span>
                            </div>
                            <textarea class="feedback-comment" placeholder="可选：请输入您的评价意见"></textarea>
                            <button onclick="submitFeedback()">提交评价</button>
                            ${result.averageRating > 0 ? 
                                `<div class="average-rating">当前服务平均评分: ${result.averageRating.toFixed(1)}/5</div>` : ''}
                        </div>
                    `;
                    // 初始化星星样式
                    updateStars(0);
                } else {
                    responseElement.innerHTML = '<p style="color: red;">抱歉，客服系统暂时无法使用，请稍后再试。</p>';
                }
            } catch (error) {
                responseElement.innerHTML = '<p style="color: red;">网络错误，请检查您的网络连接。</p>';
            }
        }

        function updateStars(rating) {
            const stars = document.querySelectorAll('.rating-stars span');
            stars.forEach((star, index) => {
                if (index < rating) {
                    star.classList.add('active');
                    star.textContent = '★';
                } else {
                    star.classList.remove('active');
                    star.textContent = '☆';
                }
            });
        }

        let currentRating = 0;

        function rateService(rating) {
            currentRating = rating;
            updateStars(rating);
        }

        function handleKeyDown(event) {
            if (event.key === 'Enter' && !event.shiftKey) {
                event.preventDefault();
                askQuestion();
            }
        }

        async function submitFeedback() {
            if (currentRating === 0) {
                alert('请先选择评分');
                return;
            }

            const comment = document.querySelector('.feedback-comment').value;
            const feedback = {
                conversationId: currentConversationId,
                rating: currentRating,
                comment: comment
            };

            try {
                const response = await fetch('/api/customer-service/feedback', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(feedback)
                });
                
                if (response.ok) {
                    const result = await response.json();
                    const averageRatingElement = document.querySelector('.average-rating');
                    if (averageRatingElement) {
                        averageRatingElement.textContent = 
                            `当前服务平均评分: ${result.averageRating.toFixed(1)}/5`;
                    }
                    alert('感谢您的评价！');
                } else {
                    alert('评价提交失败，请稍后再试');
                }
            } catch (error) {
                alert('网络错误，请检查您的网络连接');
            }
        }
    </script>
</body>
</html>